/* global window */
import queryString from 'query-string'
import { message } from 'antd'
import { routerRedux } from 'dva/router'
import { queryCreator, queryActor, update, create, remove, complete, info, submit } from 'services/project'
import { queryUser, queryGroup } from 'services/common'

export default {
  namespace: 'project',

  state: {
    users: [],
    groups: [],
    file: [],
    projectCreator: {},
    projectActor: {},
    currentItem: {},
    modalVisible: false,
    submitModalVisible: false,
    resetModalVisible: false,
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      dispatch({ type: 'queryData' })
      history.listen((location) => {
        if (location.pathname === '/project') {
          const payload = queryString.parse(location.search) || { page: 1, pageSize: 10 }
          dispatch({
            type: 'queryCreator',
            payload,
          })
          dispatch({
            type: 'queryActor',
            payload,
          })
        }
      })
    },
  },

  effects: {

    * queryData ({ payload = {} }, { call, put, select }) {
      let { users, groups } = yield select(state => state.project)
      if (groups.length === 0) {
        const { success, data, errorMsg } = yield call(queryGroup, payload)
        if (success) {
          groups = data
        } else {
          throw errorMsg
        }
      }
      if (users.length === 0) {
        const { success, data, errorMsg } = yield call(queryUser, payload)
        if (success) {
          users = data
        } else {
          throw errorMsg
        }
      }
      yield put({
        type: 'updateState',
        payload: {
          users,
          groups,
        },
      })
    },

    * queryCreator ({ payload = {} }, { call, put }) {
      const { success, data, errorMsg } = yield call(queryCreator, payload)
      if (success) {
        yield put({
          type: 'updateState',
          payload: {
            projectCreator: {
              list: data.dataList,
              pagination: {
                current: Number(payload.page) || 1,
                pageSize: Number(payload.pageSize) || 10,
                total: data.total,
              },
            },
          },
        })
      } else {
        throw errorMsg
      }
    },

    * queryActor ({ payload = {} }, { call, put }) {
      const { success, data, errorMsg } = yield call(queryActor, payload)
      if (success) {
        yield put({
          type: 'updateState',
          payload: {
            projectActor: {
              list: data.dataList,
              pagination: {
                current: Number(payload.page) || 1,
                pageSize: Number(payload.pageSize) || 10,
                total: data.total,
              },
            },
          },
        })
      } else {
        throw errorMsg
      }
    },

    * info ({ payload = {} }, { call, put }) {
      const { success, data, errorMsg } = yield call(info, payload)
      if (success) {
        yield put({
          type: payload.type,
          payload: {
            currentItem: data,
          },
        })
      } else {
        throw errorMsg
      }
    },

    * submit ({ payload = {} }, { select, call, put }) {
      const { currentItem } = yield select(_ => _.project)
      payload.projectId = currentItem.id
      const { success, data, errorMsg } = yield call(submit, payload)
      if (success) {
        message.success('提交成功')
        yield put({ type: 'hideSubmitModal' })
        yield put({ type: 'queryActor' })
      } else {
        throw errorMsg
      }
    },

    * delete ({ payload }, { call, put }) {
      const data = yield call(remove, payload)
      if (data.success) {
        yield put({ type: 'query' })
        message.success('删除成功')
      } else {
        throw data.errorMsg
      }
    },

    * save ({ payload }, { call, put }) {
      const data = yield call(payload.id ? update : create, payload)
      if (data.success) {
        message.success(payload.id ? '更新成功' : '添加成功')
        yield put({ type: 'queryCreator' })
        yield put({ type: 'hideModal' })
      } else {
        throw data.errorMsg
      }
    },

    * complete ({ payload }, { call, put }) {
      const data = yield call(complete, payload)
      if (data.success) {
        yield put(routerRedux.push('/project'))
        message.success('项目完结成功')
      } else {
        throw data.errorMsg
      }
    },

  },

  reducers: {

    updateState (state, { payload }) {
      return { ...state, ...payload }
    },

    showModal (state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal (state) {
      return { ...state, modalVisible: false, currentItem: {} }
    },

    showSubmitModal (state, { payload }) {
      return { ...state, ...payload, submitModalVisible: true }
    },

    hideSubmitModal (state) {
      return { ...state, submitModalVisible: false, currentFields: [] }
    },
  },
}
